package cn.it.facultystaff.service.impl;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

import cn.it.facultystaff.dao.FacultyStaffDao;
import cn.it.facultystaff.entity.FacultyStaff;
import cn.it.facultystaff.service.FacultyStaffService;

public class FacultyStaffServiceImpl implements FacultyStaffService {
	
	private String departmentpath="D:\\FacultyStaff\\xmldocument\\department\\department.xml";
	
	private FacultyStaffDao facultyStaffDao=new FacultyStaffDao();

	public void addFacultyStaffInfo(Map<String, String> params) {
		FacultyStaff facultyStaff=new FacultyStaff();
		facultyStaff.setDepartment(params.get("department"));
		facultyStaff.setIdNumber(params.get("IDNumber"));
		facultyStaff.setJobNumber(params.get("jobNumber"));
		facultyStaff.setName(params.get("username"));
		facultyStaff.setSex(params.get("sex"));
		facultyStaff.setPhotopath(params.get("photo"));
		
		facultyStaffDao.addXmlElement(facultyStaff);
		
	}

	public FacultyStaff getFacultyStaffByIdNumber(String jobNumber) {
		FacultyStaff facultyStaff=facultyStaffDao.findXmlElement(jobNumber);
		return facultyStaff;
	}

	public void updateFacultyStaffInfo(Map<String, String> params) {
		FacultyStaff facultyStaff=new FacultyStaff();
		facultyStaff.setJobNumber(params.get("jobNumber"));
		facultyStaff.setPhotopath(params.get("photo"));
		
		facultyStaffDao.updateXmlElement(facultyStaff);
		
	}
	public List<String> getStudentDepartment(){
		
		List<String> studentDepartments=new LinkedList<String>();
		SAXReader reader=new SAXReader();
		Document document = null;
		try {
			document = reader.read(departmentpath);
			Element departments=document.getRootElement();
			Iterator iter=departments.elementIterator();
			while(iter.hasNext()){
				Element department=(Element)iter.next();
				String nodeName=department.getName();
				if(nodeName.equals("student")){
					Iterator studentIter=department.elementIterator();
					while(studentIter.hasNext()){
						Element studentDepartment=(Element)studentIter.next();
						String departmentName=studentDepartment.getText();
						studentDepartments.add(departmentName);
					}
				}
				
			}
			
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return studentDepartments;
		
	}
	public  List<String> getTeacherDepartment(){
		
		List<String> teacherDepartments=new LinkedList<String>();
		SAXReader reader=new SAXReader();
		Document document = null;
		try {
			document = reader.read(departmentpath);
			Element departments=document.getRootElement();
			Iterator iter=departments.elementIterator();
			while(iter.hasNext()){
				Element department=(Element)iter.next();
				String nodeName=department.getName();
				if(nodeName.equals("teacher")){
					Iterator teacherIter=department.elementIterator();
					while(teacherIter.hasNext()){
						Element teacherDepartment=(Element)teacherIter.next();
						String departmentName=teacherDepartment.getText();
						teacherDepartments.add(departmentName);
					}
				}
				
			}
			
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return teacherDepartments;
		
	}
	public static void main(String[] args) {
		FacultyStaffServiceImpl fssi=new FacultyStaffServiceImpl();
		fssi.getTeacherDepartment();
	}
}
